home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cpptut22 / inherit6.cpp < prev    next >
C/C++ Source or Header  |  1992-01-19  |  3KB  |  127 lines

  1.                                   // Chapter 8 - Program 6
  2. #include <iostream.h>
  3.  
  4. class vehicle {
  5. protected:
  6.    int wheels;
  7.    float weight;
  8. public:
  9.    vehicle(void) {wheels = 7; weight = 11111.0;}
  10.    void initialize(int in_wheels, float in_weight);
  11.    int get_wheels(void) {return wheels;}
  12.    float get_weight(void) {return weight;}
  13.    float wheel_loading(void) {return weight/wheels;}
  14. };
  15.  
  16.  
  17.  
  18.  
  19. class car : public vehicle {
  20.    int passenger_load;
  21. public:
  22.    car(void) {passenger_load = 4;}
  23.    void initialize(int in_wheels, float in_weight, int people = 4);
  24.    int passengers(void) {return passenger_load;}
  25. };
  26.  
  27.  
  28.  
  29.  
  30. class truck : public vehicle {
  31.    int passenger_load;
  32.    float payload;
  33. public:
  34.    truck(void) {passenger_load = 3; payload = 22222.0;}
  35.    void init_truck(int how_many = 2, float max_load = 24000.0);
  36.    float efficiency(void);
  37.    int passengers(void) {return passenger_load;}
  38. };
  39.  
  40.  
  41.  
  42.  
  43. main()
  44. {
  45. vehicle unicycle;
  46.  
  47. // unicycle.initialize(1, 12.5);
  48.    cout << "The unicycle has " <<
  49.                                 unicycle.get_wheels() << " wheel.\n";
  50.    cout << "The unicycle's wheel loading is " <<
  51.          unicycle.wheel_loading() << " pounds on the single tire.\n";
  52.    cout << "The unicycle weighs " <<
  53.                              unicycle.get_weight() << " pounds.\n\n";
  54.  
  55. car sedan;
  56.  
  57. // sedan.initialize(4, 3500.0, 5);
  58.    cout << "The sedan carries " << sedan.passengers() <<
  59.                                                     " passengers.\n";
  60.    cout << "The sedan weighs " << sedan.get_weight() << " pounds.\n";
  61.    cout << "The sedan's wheel loading is " <<
  62.                     sedan.wheel_loading() << " pounds per tire.\n\n";
  63.  
  64. truck semi;
  65.  
  66. // semi.initialize(18, 12500.0);
  67. // semi.init_truck(1, 33675.0);
  68.    cout << "The semi weighs " << semi.get_weight() << " pounds.\n";
  69.    cout << "The semi's efficiency is " <<
  70.                           100.0 * semi.efficiency() << " percent.\n";
  71. }
  72.  
  73.  
  74.  
  75.  
  76.               // initialize to any data desired
  77. void
  78. vehicle::initialize(int in_wheels, float in_weight)
  79. {
  80.    wheels = in_wheels;
  81.    weight = in_weight;
  82. }
  83.  
  84.  
  85.  
  86. void
  87. car::initialize(int in_wheels, float in_weight, int people)
  88. {
  89.    passenger_load = people;
  90.    wheels = in_wheels;
  91.    weight = in_weight;
  92. }
  93.  
  94.  
  95.  
  96.  
  97. void
  98. truck::init_truck(int how_many, float max_load)
  99. {
  100.    passenger_load = how_many;
  101.    payload = max_load;
  102. }
  103.  
  104.  
  105.  
  106. float
  107. truck::efficiency(void)
  108. {
  109.    return payload / (payload + weight);
  110. }
  111.  
  112.  
  113.  
  114.  
  115. // Result of execution
  116. //
  117. // The unicycle has 7 wheel.
  118. // The unicycle's wheel loading is 1587.285767 pounds on the single tire.
  119. // The unicycle weighs 11111 pounds.
  120. //
  121. // The sedan carries 4 passengers.
  122. // The sedan weighs 11111 pounds.
  123. // The sedan's wheel loading is 1587.285767 pounds per tire.
  124. //
  125. // The semi weighs 11111 pounds.
  126. // The semi's efficiency is 66.666667 percent.
  127.